package cn.edu.ujn.finaltest.interceptor;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.ujn.finaltest.dao.User;

public class LoginInterceptor implements HandlerInterceptor {
	//登录验证仅在登录前进行判断即可
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {	
		String url = request.getRequestURI();
		if (url.indexOf("/login.action") >= 0) {
			return true;
		}
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("USER_SESSION"); //通过session中用户的信息来判断是否已经进行了登录操作
		if (user != null) {
			return true;
		}
		request.setAttribute("msg", "您还没有登录，请先登录！");
		request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
		return false;
	}
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
	}
	@Override
	public void afterCompletion(HttpServletRequest request,HttpServletResponse response, Object handler, Exception ex)throws Exception {
	}
}

